<?php
/**
 * Created by PhpStorm.
 * User: win7
 * Date: 2018/5/12 0012
 * Time: 9:25
 */
namespace Wx\Controller;
use Think\Controller;
class FarmerOrderController extends Controller
{
    public function index(){
        $u_id = session('u_id');
        $wholesale = M("wholesale");
        $spellgroup_copy = M("shop_spellgroup_copy");
        // 订单状态
        $state = $_GET['state'];
        //$map为拼团订单条件，$where为批发订单条件
        if($state!=''){
            if($state==3){
                $map = "sc.state >= 3";
                $where = "wh.state = 5";
            }else{
                $map =  "sc.state = ".$state;
            }//end if
            if($state==0){//批发-待付款
                $where = "wh.state = 3";
            }
            if($state==1){//批发-待发货
                $where = "wh.state = 2";
            }
            if($state==2){//批发-已发货
                $where = "wh.state = 4";
            }
            if($state==6){//批发-已发货
                $where = "wh.state = 6";
            }
        }else{
            $map = "sc.state >= 1";
            $where = "wh.state not in(0,1)";
        }//end if
        //拼团订单列表
        if($state!=0||$state!=6){//$state = 0和$state = 6均为批发订单的状态,因此不存在拼团的订单
            $copy_list = $spellgroup_copy->table(C('DB_PREFIX')."shop_spellgroup_copy as sc")
                ->join(C('DB_PREFIX')."shop_spellgroup as sg ON sg.id=sc.sg_id")
                ->where("sg.u_id = {$u_id} AND sg.state = 4 AND ".$map." AND sc.orsuccess = 1")
                ->field("sg.*,sc.state as copy_state,sc.id as copy_id,sc.num as copy_num")
                ->order('sc.id desc')
                ->select();
        }else{
            $copy_list = array();
        }
        //批发订单列表
        $wholesale_list = $wholesale->table(C('DB_PREFIX')."wholesale as wh")
                        ->join(C('DB_PREFIX')."shop_spellgroup as sg ON sg.id=wh.g_id")
                        ->field("wh.*,sg.title,sg.h_imgs,sg.unit,sg.u_num,sg.unit,sg.state as sg_state,sg.price as sg_price")
                        ->where("sg.u_id = {$u_id} AND ".$where)
                        ->order('wh.id desc')
                        ->select();
        $this->assign('wholesale_list',$wholesale_list);
        $this->assign('copy_list',$copy_list);
        $this->assign('state',$state);
        $this->display('order-nh');
    }//end fun index()

    //拼团订单详情
	public function info(){
		$id = $_GET['id'];
        $u_id = session('u_id');
        $spellgroup_copy = M("shop_spellgroup_copy");
        $shop_store = M('shop_store');
        // 拼团副本信息
        $info = $spellgroup_copy->table(C('DB_PREFIX')."shop_spellgroup_copy as sc")
            ->join(C('DB_PREFIX')."shop_spellgroup as sg ON sg.id=sc.sg_id")
            ->where("sg.u_id = {$u_id} AND sc.id = {$id}")
            ->field("sg.*,sc.state as copy_state,sc.id as copy_id,sc.num as copy_num,sc.c_id")
            ->find();
        // 收货人地址-小区门店店主信息
        $store = $shop_store->where("c_id =".$info['c_id'])->field('storename,address,realname,mobile')->find();

        $this->assign('info',$info);
        $this->assign('store',$store);
        $this->display('orderdetail-nh');
		
	}//end fun

    //发货
    public function ptShipment(){
        $spellgroup_copy = M("shop_spellgroup_copy");
        $spellgroup_order = M("shop_spellgroup_order");
        $shop_store = M('shop_store');
        $shipment = M("shipment");
        if(IS_POST){
            // 添加发货数据
            $copy_id = $_GET['copy_id'];
            $post = I('post.');
            $post['order_id'] = $copy_id;   //拼团发货时，order_id为副本id
            $post['type'] = 1;
            $post['addtime'] = time();
            $res = $shipment->add($post);
            if($res){
                // 更改副本状态为-待收货
                $spellgroup_copy->where("id={$copy_id}")->setField('state',2);
                $this->success('发货成功','/Wx/FarmerOrder/info/id/'.$copy_id);
            }
        }else{
            $u_id = session('u_id');
            $id = $_GET['id'];  //订单id
            // 拼团副本信息
            $info = $spellgroup_copy->table(C('DB_PREFIX')."shop_spellgroup_copy as sc")
                ->join(C('DB_PREFIX')."shop_spellgroup as sg ON sg.id=sc.sg_id")
                ->where("sg.u_id = {$u_id} AND sc.id = {$id}")
                ->field("sg.*,sc.state as copy_state,sc.id as copy_id,sc.num as copy_num,sc.c_id")
                ->find();
            // 收货人地址-小区门店店主信息
            $store = $shop_store->where("c_id =".$info['c_id'])->field('storename,address,realname,mobile')->find();

            $this->assign('info',$info);
            $this->assign('store',$store);
            $this->display('shipment02');
        }

    }//end fun



    // 批发订单详情
    public function saleInfo(){
        $u_id = session('u_id');
        $wholesale = M("wholesale");
        $id = $_GET['id'];  //订单id
        //批发订单列表
        $sale_info = $wholesale->table(C('DB_PREFIX')."wholesale as wh")
                    ->join(C('DB_PREFIX')."shop_spellgroup as sg ON sg.id=wh.g_id")
                    ->field("wh.*,sg.price as sg_price,sg.title,sg.h_imgs,sg.unit,sg.u_num,sg.unit,sg.state as sg_state")
                    ->where("sg.u_id = {$u_id} AND wh.id = {$id}")
                    ->find();
        $this->assign('sale_info',$sale_info);
        $this->display('orderdetail-nh02');
    }

    // 批发-发货
    public function pfShipment(){
        $wholesale = M("wholesale");
        $shipment = M("shipment");
        if(IS_POST){
            // 添加发货数据
            $order_id = $_GET['order_id'];
            $post = I('post.');
            $post['order_id'] = $order_id;
            $post['type'] = 2;
            $post['addtime'] = time();
            $res = $shipment->add($post);
            if($res){
                //更改订单状态
                $wholesale->where("id={$order_id}")->setField('state',4);
                $this->success('发货成功','/Wx/FarmerOrder/saleInfo/id/'.$order_id);
            }
        }else{
            $u_id = session('u_id');
            $id = $_GET['id'];  //订单id
            //批发订单列表
            $info = $wholesale->table(C('DB_PREFIX')."wholesale as wh")
                ->join(C('DB_PREFIX')."shop_spellgroup as sg ON sg.id=wh.g_id")
                ->field("wh.*,sg.price as sg_price,sg.title,sg.h_imgs,sg.unit,sg.u_num,sg.unit,sg.state as sg_state")
                ->where("sg.u_id = {$u_id} AND wh.id = {$id}")
                ->find();
            $this->assign('info',$info);
            $this->display('shipment');
        }

    }// end fun shipment()
}//end class